﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace AllInOne.Classes
{
    /// <summary>
    /// 称重状态转换器
    /// </summary>
    public class WeightStatusConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string result;
            switch (value.ToString())
            {
                case "1": result = "未超重"; break;
                case "2": result = "超重"; break;
                case "3": result = "人工确认未超重"; break;
                default: result = ""; break;
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }

    /// <summary>
    /// 连接状态转换器
    /// </summary>
    public class LinkStateConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int state = System.Convert.ToInt32(value);
            if (state==1) return "正常";
            else if (state == 2) return "未启用";
            else return "断开";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }

    public class DateConvert : IValueConverter
    {
        /// <summary>
        /// 源属性传给目标属性时，调用此方法Convert
        /// </summary>
        /// <param name="value">数据源</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">不常用</param>
        /// <param name="culture">不常用</param>
        /// <returns>目标值</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return DependencyProperty.UnsetValue;
            DateTime date = (DateTime)value;
            return date.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 目标属性传给源属性时，调用此方法ConvertBack
        /// </summary>
        /// <param name="value">目标值</param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>数据源</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = value as string;
            DateTime txtDate;
            if (DateTime.TryParse(str, out txtDate))
                return txtDate;
            return DependencyProperty.UnsetValue;
        }
    }


    public class DateTimeConvert : IValueConverter
    {
        /// <summary>
        /// 源属性传给目标属性时，调用此方法Convert
        /// </summary>
        /// <param name="value">数据源</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">不常用</param>
        /// <param name="culture">不常用</param>
        /// <returns>目标值</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return DependencyProperty.UnsetValue;
            DateTime date = (DateTime)value;
            return date.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 目标属性传给源属性时，调用此方法ConvertBack
        /// </summary>
        /// <param name="value">目标值</param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>数据源</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string str = value as string;
            DateTime txtDate;
            if (DateTime.TryParse(str, out txtDate))
                return txtDate;
            return DependencyProperty.UnsetValue;
        }
    }
}
